$(function() {
    $("#oldPassword2").change(function () {
        var $oldPassword1 = $("#oldPassword1").val();
        var $oldPassword2 = $("#oldPassword2").val();

        if ($oldPassword2 == "") {
            layer.msg("请输入旧密码!", {
                offset: 't',
                anim: 6
            });
            return;
        }
        if ($oldPassword1 != $oldPassword2){
            layer.msg("旧密码输入不正确!", {
                offset: 't',
                anim: 6
            });
            return;
        }
    });

    $("#newPassword").change(function () {
        var $newPassword = $("#newPassword").val();
        var reg=/^[0-9a-zA-Z]{6,15}$/;

        if ($newPassword == "") {
            layer.msg("请输入新密码!", {
                offset: 't',
                anim: 6
            });
            return;
        }
        if(!reg.test($newPassword)){
            layer.msg("新密码不符合要求!", {
                offset: 't',
                anim: 6
            });
            return;
        }
    });

    $("#save-btn").on("click",function() {
        var $oldPassword1 = $("#oldPassword1").val();
        var $oldPassword2 = $("#oldPassword2").val();
        var $newPassword = $("#newPassword").val();
        var $reNewPassword = $("#reNewPassword").val();
        var reg=/^[0-9a-zA-Z]{6,15}$/;

        if ($oldPassword2 == "") {
            layer.msg("请输入旧密码!", {
                offset: 't',
                anim: 6
            });
            return;
        }
        if ($oldPassword1 != $oldPassword2){
            layer.msg("旧密码输入不正确!", {
                offset: 't',
                anim: 6
            });
            return;
        }
        if ($newPassword == "") {
            layer.msg("请输入新密码!", {
                offset: 't',
                anim: 6
            });
            return;
        }
        if (!reg.test($newPassword)){
            layer.msg("新密码不符合要求!", {
                offset: 't',
                anim: 6
            });
            return;
        }
        if ($reNewPassword =="") {
            layer.msg("请确认新密码!", {
                offset: 't',
                anim: 6
            });
            return;
        }
        if ($newPassword!=$reNewPassword) {
            layer.msg("新密码前后不一致!", {
                offset: 't',
                anim: 6
            });
            return;
        }

        $("#save-form").submit();
    });

    $(document).on("keydown",function(e) {
        if (e.keyCode == 13) {
            $("#save-btn").trigger("click");
        }
    });

});